define(
	[
		'require'
	],
	function(require){
		var Controller = Ember.Controller.extend({
			
			isTrue: false,

			typeValue: '',

			channelValue: '',

			keyWord: '',
			trackColumns: [
				{
					headerText: "序号",
					dataKey:"sequence",
					width: 60,
					allowSort: false,
					ensurePxWidth: true,
					
				},
				{
					headerText: "发起时间",
					dataKey:"startTime",
					allowSort: false,
					ensurePxWidth: true,
					cellFormatter: function (args, colume) {
						if (args.row.type & $.wijmo.wijgrid.rowType.data) {
							var ctrl = this.get("controller");
							var date = new Date(args.row.data.startTime);
							var dateFor=APM.util.formatDate(date, "yyyy-MM-dd H:mm:ss");
							var cb = $("<div class='date-text'></div>").text(dateFor);
							cb.appendTo(args.$container);
							return true;
						}
					}
				},
				{
					headerText: "交易号",
					dataKey:"code",
					allowSort: false,
					ensurePxWidth: true,
					
				},
				{
					headerText: "交易类型",
					dataKey:"typeName",
					allowSort: false,
					
				},
				{
					headerText: "客户端IP",
					dataKey:"srcIp",
					allowSort: false,
					
				},
				{
					headerText: "服务端IP",
					dataKey:"dstIp",
					allowSort: false,
					
				},
				{
					headerText: "返回码",
					dataKey:"responseCode",
					allowSort: false,
					
				},
				{
					headerText: "响应时间",
					dataKey:"responseTime",
					_fmtType: "transform_time",
					cellFormatter: APM.util.indicatorCellFormatter
					
				},
				{
					headerText: "交易结果",
					dataKey:"result",
					allowSort: false,
					
				}
			],

			alerts: Ember.A(),

			transactionChannel: Ember.A(),

			transactionType:Ember.A(),

			isOnce:true,

			requireAlerts: function(params) {
				var ctrl = this,
					serviceId = ctrl.serviceId(),
					channelValue = this.get("channelValue"),
					typeValue = this.get("typeValue"),
					isTrue = ctrl.get("isTrue");
					
					ctrl.set('channelValue','');
					ctrl.set('typeValue','');	
				var startDate = this.get("startDate"),
					endDate = this.get("endDate");

				if( isLocalDebugModel ){
					endDate = new Date(2014, 9, 1, 12, 0, 0);
					startDate = new Date(+endDate)
					startDate.setHours(startDate.getHours()-1);
				}
		
				var params = {
					"serviceId":ctrl.get("model.id"),
					"keyWord": this.get("keyWord"),
				    "startDate": +startDate,
				    "pageIndex": this.get("pageIndex"),
				    "pageSize": this.get("pageSize"),
					"endDate": +endDate,
					"transactionChannel":channelValue,
					"transactionType": typeValue
				};
				//开发调试模式参数
				if( isLocalDebugModel ){
					params = undefined;
				}

				if(isTrue){

					ctrl.mergeTransactionRequireAlerts(params)
					.then(function(response){
						var status = response.status;
						if(status != "success"){
							return
						};
						ctrl.set('isData',false);
						var alerts = response.result;
						
						 alerts.forEach(function(node,index){
							node.sequence = index+1;
						});

						if (alerts.length == 0) {
							ctrl.set("pageIndex",0);
							ctrl.set("pageSize",10);
							ctrl.set("totalRows",0);
						} else {
							ctrl.set("pageIndex",response.meta.pageIndex);
							ctrl.set("pageSize",response.meta.pageSize);
							ctrl.set("totalRows",response.meta.total);
						}
						
						ctrl.set("alerts", alerts);


						return response;
					});
					return;
				}
				return ctrl.store.ajax("GET", "trades/tracking", params)
				.then(function(response){
					var status = response.status;
					if(status != "success"){
						return
					};

					ctrl.set('isData',false);

					var alerts = response.result;
					alerts.forEach(function(node,index){
						node.sequence = index+1;
						if(node.isError){
							node.result = "失败";
						}else{
							node.result = "成功";
 						}
					});		

					if (alerts.length == 0) {
							ctrl.set("pageIndex",0);
							ctrl.set("pageSize",10);
							ctrl.set("totalRows",0);
						} else {
							ctrl.set("pageIndex",response.meta.pageIndex);
							ctrl.set("pageSize",response.meta.pageSize);
							ctrl.set("totalRows",response.meta.total);
						}
					
					ctrl.set("alerts", alerts);
					return response;
				});
			},
			serviceId: function(){
				var serviceId = this.get("model.id");
				var arrId = serviceId.split('-');
				return serviceId = arrId[arrId.length-1];
			},
			transactionChannelRequireAlerts: function(params){
				return this.store.ajax("GET","transaction/channel",params);
			},
			transactionTypeRequireAlerts: function(params){
				return this.store.ajax("GET","transaction/type",params);
			},
			mergeTransactionRequireAlerts: function(params){
				return this.store.ajax("GET","trades/tracking/merge",params)
			},
			mergeTransaction: Ember.observer('isTrue',function(){
				if(this.get('isTrue')){
					this.requireAlerts();
				}else{	
					this.requireAlerts();
				}
			}),
			actions: {
				queryBusinessNum:function(){
					this.set("pageIndex",0);
					this.requireAlerts();
				},
				pageIndexChanged: function(e, data, view){
					var pageNo = data.newPageIndex;
					this.set("pageIndex", pageNo);

					this.requireAlerts();
				},
				timeRangesChange: function(timeRange){
					var ctrl = this,
						locationStart = this.get("locationStart");
					
					// if(!isSelectedTime){
						ctrl.set("startDate", timeRange.startDate);
						ctrl.set("endDate", timeRange.endDate);
					// }else{
					// 	var view = Broada.getView("businessTimeRangesTopo");
					// 	if(this.get("clickTimePoint") != null){
					// 		this.set("locationStart",true);
					// 		var date = new Date(this.get("clickTimePoint"));
					// 		if(typeof view != "undefined"){
					// 			var range = view.getTimeRange().endDate - view.getTimeRange().startDate;	
					// 			if(range == 60000){
					// 				this.set("startDate", date);
					// 				this.set("endDate", new Date(date.getTime()+range));
					// 			}else{
					// 				this.set("startDate", new Date(date-range));
					// 				this.set("endDate", date);
					// 			}
					// 		}
					// 		typeof view != "undefined"?view.set("btnTxt", date?APM.util.formatDate(date, "f"):null):null;
					// 		ctrl.requireAlerts();
					// 	}
					// };
					
					ctrl.set("pageIndex", 0);
					ctrl.requireAlerts();
					
				},
				mergeTransactionBox: function(){
					
				},
				mergeTransaction: function(){
					var ctrl = this,
						isTrue = ctrl.get('isTrue');

					if(isTrue){
						ctrl.set('isTrue',false);
					}else{
						ctrl.set('isTrue',true);
					}
				}
			}
		});
	
		APM.ServiceServerNodeserverTrackController = Controller;

		return Controller;
	}
);	